package com.yuexun.dhdoctor.module.Contract.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.adapter.CreateGroupAdapter;
import com.yuexun.dhdoctor.module.Contract.bean.DeleteGroupBean;
import com.yuexun.dhdoctor.module.Contract.bean.DepartOrPeopleData;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;
import com.yuexun.dhdoctor.module.Contract.bean.People;
import com.yuexun.dhdoctor.module.Contract.model.impl.ManageGroupModel;
import com.yuexun.dhdoctor.module.Contract.presenter.IManageGroupPresenter;
import com.yuexun.dhdoctor.module.Contract.presenter.impl.ManageGroupPresenter;
import com.yuexun.dhdoctor.module.Contract.ui.view.IManageGroupView;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/7/26.
 */
public class Act_ManageGroup extends BaseKJActivity implements IManageGroupView {
    @Bind(R.id.mangroup_name_edt)
    EditText mangroupNameEdt;
    @Bind(R.id.mangroup_count)
    TextView mangroupCount;
    @Bind(R.id.grid)
    GridView grid;

    ArrayList<People> usersBeans_origin = new ArrayList<People>();
    GroupGroupData groupGroupDataBeans = new GroupGroupData();

    CreateGroupAdapter adapter;

    IManageGroupPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_managegroup);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new ManageGroupPresenter(new ManageGroupModel(), this);
        groupGroupDataBeans = (GroupGroupData) getIntent().getExtras().getSerializable("groupinfo");
        for (int i = 0; i < groupGroupDataBeans.getToId().size(); i++) {
            People people = new People();
            people.setUserId(groupGroupDataBeans.getToId().get(i).toString());
            people.setUserName(groupGroupDataBeans.getToName().get(i).toString());
            usersBeans_origin.add(people);
        }
        setGridView();
        mangroupNameEdt.setText(groupGroupDataBeans.getTitle());
    }

    private void setGridView() {

        int size = usersBeans_origin.size();

        int length = 80;

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
        grid.setLayoutParams(params); // 重点
        grid.setColumnWidth(itemWidth); // 重点
        grid.setHorizontalSpacing(5); // 间距
        grid.setStretchMode(GridView.NO_STRETCH);
        grid.setNumColumns(size); // 重点

        List<DepartOrPeopleData> datas = new ArrayList<>();
        for (People people : usersBeans_origin) {
            DepartOrPeopleData data = new DepartOrPeopleData();
            data.setDataId(people.getUserId() + "");
            data.setDataName(people.getUserName());
            datas.add(data);
        }

        adapter = new CreateGroupAdapter(grid, datas, R.layout.item_send_addmember);
        grid.setAdapter(adapter);

        mangroupCount.setText(size + "人");
    }

    @OnClick({R.id.back, R.id.mangroup_save, R.id.mangroup_add, R.id.mangroup_delete, R.id.mangroup_deleteall})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.mangroup_save:
                String memberIds = null, names = null;

                for (int i = 0; i < usersBeans_origin.size(); i++) {
                    if (i == 0) {
                        memberIds = usersBeans_origin.get(i).getUserId();
                        names = usersBeans_origin.get(i).getUserName();
                    } else {
                        memberIds = memberIds + ";" + usersBeans_origin.get(i).getUserId();
                        names = names + ";" + usersBeans_origin.get(i).getUserName();
                    }
                }

                Map<String, String> params_delete = new HashMap<>();
                params_delete.put("sessionUuid", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params_delete.put("groupId", groupGroupDataBeans.getGroupId());
                params_delete.put("title", mangroupNameEdt.getText().toString());
                params_delete.put("memberIds", memberIds);
                params_delete.put("names", names);
                presenter.updateGroup(params_delete);
                break;
            case R.id.mangroup_add:
                Bundle bundle = new Bundle();
                Intent intent = new Intent(this, Act_AddMember.class);
                bundle.putSerializable("persons", usersBeans_origin);
                intent.putExtras(bundle);
                aty.startActivityForResult(intent, 1);
                break;
            case R.id.mangroup_delete:
                Bundle bundle_delete = new Bundle();
                Intent intent_delete = new Intent(this, Act_DeleteMember.class);
                bundle_delete.putSerializable("persons", usersBeans_origin);
                intent_delete.putExtras(bundle_delete);
                aty.startActivityForResult(intent_delete, 2);
                break;
            case R.id.mangroup_deleteall:
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("groupId", groupGroupDataBeans.getGroupId());
                presenter.deleteGroup(params);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                usersBeans_origin = (ArrayList<People>) data.getExtras().getSerializable("persons");
                setGridView();
                break;
            case 101:
                usersBeans_origin = (ArrayList<People>) data.getExtras().getSerializable("persons");
                setGridView();
                break;
            default:
                break;
        }
    }

    @Override
    public void deleteGroup(DeleteGroupBean deleteGroupBean) {
        if ("200".equals(deleteGroupBean.getStatus())) {
            ViewInject.toast("删除群组");
            finish();
        } else {
            ViewInject.toast("删除群组失败");
        }
    }

    @Override
    public void updateGroup(DeleteGroupBean deleteGroupBean) {
        if ("200".equals(deleteGroupBean.getStatus())) {
            ViewInject.toast("更新群组");
            finish();
        } else {
            ViewInject.toast("更新群组失败");
        }
    }
}
